package com.internetCafes.spms.web.customer.common.component.customer.impl;

import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.customer.bizservice.customer.IEntCustomerBizService;
import com.internetCafes.spms.web.customer.common.component.customer.IEntCustomerComponent;
import com.internetCafes.spms.web.customer.common.component.msg.impl.MsgComponent;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.common.cont.CustomerConst;
import com.internetCafes.spms.web.customer.entity.customer.Customer;
import com.internetCafes.spms.web.customer.model.ExcelImportResultRes;
import com.internetCafes.spms.web.customer.model.customer.CustomerBatchSaveReq;
import com.internetCafes.spms.web.customer.model.customer.CustomerSaveReq;
import com.internetCafes.spms.web.customer.model.customer.CustomerUpdateReq;
import com.internetCafes.spms.web.customer.service.customer.ICustomerFlowService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerService;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import com.sms.common.util.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 意向客户-个人
 */
@Component("intentionPersonCustomerComponent")
public class IntentionPersonCustomerComponent implements IEntCustomerComponent {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomerFlowService flowService;
    @Autowired
    private MsgComponent msgComponent;
    @Autowired
    private IEntCustomerBizService customerBizService;

    /**
     * 列表名称
     */
    private final String MENU_NAME = "意向个人";

    @Override
    public ExcelImportResultRes batchSave(CustomerBatchSaveReq req) {
        return customerBizService.personBatchSave(req, MENU_NAME);
    }

    @Override
    public boolean update(CustomerUpdateReq req) {
        // 必填字段判断
        if (StrUtil.isBlank(req.getName()) || ObjectUtil.isNull(req.getCityId())
                || ObjectUtil.isNull(req.getAreaId()) || ObjectUtil.isNull(req.getId())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }

        // 保存客户信息
        Customer customerDo = new Customer();
        BeanUtil.copyProperties(req, customerDo);
        customerDo.setContactsName(req.getName());
        return customerService.updateById(customerDo);
    }

    @Override
    public boolean save(CustomerSaveReq req) {
        // 必填字段判断
        if (StrUtil.isBlank(req.getName()) || ObjectUtil.isNull(req.getCityId()) || ObjectUtil.isNull(req.getAreaId())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }

        // 保存客户信息
        Customer customerDo = new Customer();
        BeanUtil.copyProperties(req, customerDo);
        customerDo
                .setContactsName(req.getName())
                .setMainlyFlag(CommonConst.FlagEnum.NOT.getId())
                .setThisYearDealFlag(CommonConst.FlagEnum.NOT.getId())
                .setLastYearDealFlag(CommonConst.FlagEnum.NOT.getId())
                .setFollowUserId(ShiroUtils.getUserId());
        customerService.save(customerDo);

        // 创建记录
        flowService.add(customerDo.getId(), CustomerConst.FlowActionTag.CREATE.getId(),
                null, null, ShiroUtils.getUserId(),
                ShiroUtils.getUserEntity().getNickName());
        return true;
    }

}
